package com.faner.fast.exception;

import com.faner.fast.constant.enums.CodeEnum;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 参数错误异常
 * description: ParameterException
 * date: 2020/11/20 16:18
 * author: faner
 */
public class ParameterException extends HttpException{

    protected int httpCode = HttpStatus.BAD_REQUEST.value();

    protected int code = CodeEnum.PARAMETER_ERROR.getCode();

    private Map<String, Object> errors = new HashMap<>();

    public ParameterException() {
        super(CodeEnum.PARAMETER_ERROR.getCode(), CodeEnum.PARAMETER_ERROR.getMessage());
    }

    public ParameterException(String message) {
        super(message);
    }

    public ParameterException(int code) {
        super(code, CodeEnum.PARAMETER_ERROR.getMessage());
        this.code = code;
    }


    public ParameterException(int code, String message) {
        super(code, message);
        this.code = code;
    }

    public ParameterException(Map<String, Object> errors) {
        this.errors = errors;
    }

    public ParameterException addError(String key, Object val) {
        this.errors.put(key, val);
        return this;
    }

    @Override
    public String getMessage() {
        if (errors.isEmpty()) {
            return super.getMessage();
        }
        return errors.toString();
    }

    @Override
    public int getHttpCode() {
        return httpCode;
    }

    @Override
    public int getCode() {
        return code;
    }
}
